package campus.aiit.ssl.util.Factory;

import campus.aiit.ssl.dao.AlarmDao;
import campus.aiit.ssl.domain.model.Alarm;
import campus.aiit.ssl.domain.model.CollectLog;
import campus.aiit.ssl.domain.model.Message;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;

@Component
public class MessageFactory {
    private Alarm[] alarms;

    public MessageFactory(AlarmDao alarmDao){
        this.alarms = alarmDao.selectAlarms();
    }

    public Message getMessage(CollectLog collectLog) {
//        System.out.println(Arrays.toString(alarms));
        Message message = new Message();
        message.setStatus(0);
        if (collectLog.getAmperes()>=alarms[0].getValue()) {
            message.setAlarmId(alarms[0].getId());
            message.setContent(alarms[0].getName());
            message.setDeviceId(collectLog.getDeviceId());
        }
        if (collectLog.getAmperes()<=alarms[1].getValue()) {
            message.setAlarmId(alarms[1].getId());
            message.setContent(alarms[1].getName());
            message.setDeviceId(collectLog.getDeviceId());
        }
        if (collectLog.getContextT()>=alarms[2].getValue()) {
            message.setAlarmId(alarms[2].getId());
            message.setContent(alarms[2].getName());
            message.setDeviceId(collectLog.getDeviceId());
        }
        if (collectLog.getContextT()<=alarms[3].getValue()) {
            message.setAlarmId(alarms[3].getId());
            message.setContent(alarms[3].getName());
            message.setDeviceId(collectLog.getDeviceId());
        }
//        System.out.println(message);
        return message;
    }
}
